#/bin/bash
#用于修改Docker配置的脚本

##你可以在这里找到:
#镜像修改
#存储驱动修改
#远程访问开启与关闭
#添加私有仓库地址
check_docker_installed(){
if [[ -f "/usr/sbin/docker" ]];then
    input "貌似本机未安装Docker,确定继续[y/n],默认[ n ]" c
    if [[ "$c" -eq 'y' || "$c" -eq 'Y' ]];then
        green_zt "好吧，既然你都坚持了"
    else
        exit 1
    fi
fi
}
tools_Dockr_tools_Docker__reset_mirrors(){
    mkdir -p /etc/docker/
    input "请选择一个Docker镜像加速站\n[1]网易\n[2]阿里云\n[3]Docker-CN\n[4]腾讯\n[5]中国科技大学" mirror
    case $mirror in 
        '1')
            echo -e "{\n\t\"registry-mirrors\": [\"http://hub-mirror.c.163.com\"]\n}" > /etc/docker/daemon.json
            ;;
        '2')
            echo -e "{\n\t\"registry-mirrors\":[\"https://ung2thfc.mirror.aliyuncs.com\"]\n}" > /etc/docker/daemon.json
            ;;
        '3')
            echo -e "{\n\t\"registry-mirrors\": [\"https://registry.docker-cn.com\"]\n}" > /etc/docker/daemon.json
            ;;
        '4')
            echo -e "{\n\t\"registry-mirrors\": [\"https://mirror.ccs.tencentyun.com\"]\n}" > /etc/docker/daemon.json
            ;;
        '5')
            echo -e "{\n\t\"registry-mirrors\": [\"https://docker.mirrors.ustc.edu.cn\"]\n}" > /etc/docker/daemon.json
            ;;
    esac
    chek_status "$?" "Docker镜像加速配置写入"
    service_start "docker"
    green_zt "Docker镜像加速修改完成"
    docker info
}
#Docker存储驱动修改功能
tools_Dockr_tools_Docker__Storage_driver(){
    #1参选择配置模式
    #2参选择配置的驱动类型
    mkdir -p /etc/docker/
    if [[ -f "/usr/sbin/docker" ]];then
        input "貌似本机未安装Docker,确定继续[y/n],默认[ n ]" c
        if [[ "$c" -eq 'y' || "$c" -eq 'Y' ]];then
            green_zt "好吧，既然你都坚持了"
        else
            exit 1
        fi
    fi
    if [[ -z "$1" ]];then
        input "请选择修改方式[只能删除现有配置(有备份策略)]\n[1]多配置,默认(配置镜像加速及驱动）\n[2]单独配置(只设置驱动)" fs
    else
        fs="$1"
    fi
    if [[ -z "$2" ]];then
        input "需要把驱动改为:\n[1]system[默认]\n[2]cgroupfs" Driver
    else
        Driver="$2"
    fi
    if [[ $fs -eq '2' ]];then 
        echo -e '{\n\t"exec-opts": ["native.cgroupdriver=systemd"]\n}' > /etc/docker/daemon.json
    else
        if [[ -f '/etc/docker/daemon.json' ]];then
            if [[ -f '/etc/docker/daemon.json.bak' ]];then
                green_zt "已备份[ /etc/docker/daemon.json.bak ]"
            else
                cp /etc/docker/daemon.json /etc/docker/daemon.json.bak
            fi
        fi
        reload_Docker_mirrors #配置镜像加速
        sed -i 's/}//g' /etc/docker/daemon.json
        sed -i 's/\]/\],/g' /etc/docker/daemon.json
        echo -e '\t"exec-opts": ["native.cgroupdriver=systemd"]\n}' >> /etc/docker/daemon.json
        service_start 'docker'
    fi
    if [[ $Driver -eq '2' ]];then
        sed -i 's/systemd/cgroupfs/' /etc/docker/daemon.json
        Drivers=cgroupfs
    fi
    Drivers=systemd
    service_start 'docker'
    chek_status "$?" "配置Docker驱动 [ $Drivers ]"
}
#Docker远程访问开启与关闭
tools_Dockr_tools__Docker_remote(){
    input "请选择菜单\n[1]开启远程访问\n[2]关闭远程访问" remote
    case $remote in
        '1')
            echo '开启'
            ;;
        '2')
            echo '关闭'
            ;;
    esac
}
#添加私有仓库地址
tools_Dockr_tools_Docker_Private_mirrors(){
    IP="$1"
    if [[ -e '/usr/lib/systemd/system/docker.service' ]];then
        if [[ -e '/usr/lib/systemd/system/docker.service.bak' ]];then
            green_zt '已备份[ usr/lib/systemd/system/docker.service.bak ]'
            cp /usr/lib/systemd/system/{docker.service.bak,docker.sersvice}
        else
            cp /usr/lib/systemd/system/{docker.service,docker.service.bak}
        fi
        if [[ -z `cat /usr/lib/systemd/system/docker.service | grep 'insecure-registry'` ]];then
            sed -i "s/dockerd/dockerd  --insecure-registry  ${IP}:5000/" /usr/lib/systemd/system/docker.service
        else
            red_z_green_z "检测到已配置私有镜像仓库" "[`cat /usr/lib/systemd/system/docker.service | grep 'insecure-registry'`] "
            input "是否修改地址为:[ ${IP}:5000 ],输入[ y/n],默认[ y ]" set_url
            input_check "$set_url" "私有镜像地址修改" "y" "set_url"
            if [[ "$set_url" == 'y' ]];then
                sed "s@insecure-registr.*:5000@insecure-registry ${IP}:5000@"  /usr/lib/systemd/system/docker.service
            else
                green_zt "跳过修改"
            fi
        fi
    fi
    service_start 'docker'
}